package com.darco.darcoapp;

import com.darco.darcoapp.db.DarcoData;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;
import static DARCOShared.utils.Constants.*;

public class Settings_Activity extends Activity implements OnClickListener{

	private DarcoData d;
	private Bundle extra=null;
	private ContentValues account=null;
	private ContentValues system=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings_);


		extra=getIntent().getExtras();
		if(extra!=null){
			account = ((ContentValues) extra.get("account"));
			system = ((ContentValues) extra.get("system"));
		}

		d=new DarcoData(this); //nuovo oggetto db, accede allo stesso file

		View allowLoc = findViewById(R.id.settings_allowloc_toggleButton);
		View clearDb = findViewById(R.id.settings_deleteLocalDb_Button);

		//imposto il toggle button sullo stato corrente
		if(system!=null){
			boolean statusToggle=false;
			if(system.getAsInteger(SYSTEM_ALLOWLOCATION)!=0)
				statusToggle=true;
			
			((ToggleButton) allowLoc).setChecked(statusToggle);
		}

		//setto i click listener
		allowLoc.setOnClickListener(this);
		clearDb.setOnClickListener(this);


	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_settings_, menu);


		return true;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.settings_allowloc_toggleButton:
			boolean ck=((ToggleButton) v).isChecked();
			boolean result = false;
			if(system!=null)
				result = Settings.setAllowLocation(d, system, ck);
			else{
				result = false;				
			}

			String status="Deactivate";
			if(ck==true)
				status="Activate";
			Toast.makeText(this, "Localization status: " +status, Toast.LENGTH_SHORT).show();
			if(result==false){
				Log.d("Localization = "+status,"but not saved into db");
			}

			break;
		case R.id.settings_deleteLocalDb_Button:
			Settings.clearLocalDb(d);
			Toast.makeText(this, "Local database Cleared", Toast.LENGTH_SHORT).show();
		default:
			break;
		}

	}
}
